import GCConnection from "../../GCConnection";
import GCGlobal from "../../GCGlobal";
import { AccountResponse } from "../../model/model-account";
import GCNetwork from "../../network/GCNetwork";
import GCToast from "../common-components/GCToast";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Loading extends cc.Component {


    @property(cc.ProgressBar)
    loading: cc.ProgressBar = null;

    protected onLoad(): void {
        this.loading.progress = 0;
        GCGlobal.initGlobal();
        GCNetwork.initNetwork();
    }

    protected start(): void {
        this.startLoad();
    }

    startLoad() {
        const duration = Math.random() * 0.2 + .6;
        cc.tween(this.loading)
            .to(duration, { progress: 1 }, { easing: 'quadOut' })
            .call(() => {
                this.onLoadCompleted();
            })
            .start();
    }

    onLoadCompleted() {
        this.doH5Login();
    }

    doH5Login() {
        if (GCGlobal.isSignedIn()) {
            GCConnection.userinfo()
                .then((resp: AccountResponse) => {

                    GCGlobal.Account = resp;
                    cc.director.loadScene('main');
                    cc.log(resp);

                }).catch(e => {

                    cc.error(e);
                    cc.director.loadScene('login')

                });
        } else {

            cc.director.loadScene('login');

        }
    }
}
